package com.mjk.common.tools.scheduled.conf;

import com.mjk.common.tools.scheduled.single.impl.DefaultSingelPointImpl;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;


@Configuration
@EnableScheduling
public class SecheduleConfig implements SchedulingConfigurer,BeanDefinitionRegistryPostProcessor{
	
	BeanDefinitionRegistry registry;

	//private String application = "application";

	@Override
	public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
	    scheduledTaskRegistrar.setScheduler(setTaskExecutors());
	}
	
	
		
	@Bean
	public Executor setTaskExecutors(){
	    return Executors.newScheduledThreadPool(20); // 20个线程来处理。
	}
	
	

	@Override
	public void postProcessBeanFactory(
			ConfigurableListableBeanFactory beanFactory) throws BeansException {
		AbstractBeanDefinition beanDefinition;
//		if(beanFactory.containsBean("dccProperty")){
//			BeanDefinitionBuilder builder= BeanDefinitionBuilder.rootBeanDefinition(ZKSingelPointImpl.class);
//			builder.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
//			beanDefinition = builder.getBeanDefinition();
//	        ConstructorArgumentValues values = new ConstructorArgumentValues();
//	        String zkHost = ResourceUtils.getResource(getApplicationName()).getMap().get("bee.dubbo.registry.address");
//	        DccProperty property = beanFactory.getBean("dccProperty",DccProperty.class);
//	        values.addIndexedArgumentValue(0, ObjectUtils.isEmpty(property.getZkHost())?zkHost:property.getZkHost());
//	        values.addIndexedArgumentValue(1, property.getConnectTimeout());
//	        beanDefinition.setConstructorArgumentValues(values);
//		}else if(beanFactory.containsBean("redisConfig")){
//			BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(RedisSingelPointImpl.class);
//			builder.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
//			beanDefinition = builder.getBeanDefinition();
//		}
//		else{
			BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(DefaultSingelPointImpl.class);
			builder.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
			beanDefinition = builder.getBeanDefinition();
//		}
		//if(null!=beanDefinition)
			registry.registerBeanDefinition("single", beanDefinition);
	}

	@Override
	public void postProcessBeanDefinitionRegistry(
			BeanDefinitionRegistry registry) throws BeansException {	        
		this.registry = registry;
	}

}
